package com.ruoyi.engine.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.engine.mapper.BpmProcessDefinitionMapper;
import com.ruoyi.engine.domain.BpmProcessDefinition;
import com.ruoyi.engine.service.IBpmProcessDefinitionService;

/**
 * 模版引擎管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-25
 */
@Service
public class BpmProcessDefinitionServiceImpl implements IBpmProcessDefinitionService 
{
    @Autowired
    private BpmProcessDefinitionMapper bpmProcessDefinitionMapper;

    @Override
    public Integer selectMaxVersion() {
        return bpmProcessDefinitionMapper.selectMaxVersion();
    }

    /**
     * 查询模版引擎管理
     * 
     * @param id 模版引擎管理主键
     * @return 模版引擎管理
     */
    @Override
    public BpmProcessDefinition selectBpmProcessDefinitionById(Long id)
    {
        return bpmProcessDefinitionMapper.selectBpmProcessDefinitionById(id);
    }

    /**
     * 查询模版引擎管理列表
     * 
     * @param bpmProcessDefinition 模版引擎管理
     * @return 模版引擎管理
     */
    @Override
    public List<BpmProcessDefinition> selectBpmProcessDefinitionList(BpmProcessDefinition bpmProcessDefinition)
    {
        return bpmProcessDefinitionMapper.selectBpmProcessDefinitionList(bpmProcessDefinition);
    }

    /**
     * 新增模版引擎管理
     * 
     * @param bpmProcessDefinition 模版引擎管理
     * @return 结果
     */
    @Override
    public int insertBpmProcessDefinition(BpmProcessDefinition bpmProcessDefinition)
    {
        bpmProcessDefinition.setCreateTime(DateUtils.getNowDate());
        return bpmProcessDefinitionMapper.insertBpmProcessDefinition(bpmProcessDefinition);
    }

    /**
     * 修改模版引擎管理
     * 
     * @param bpmProcessDefinition 模版引擎管理
     * @return 结果
     */
    @Override
    public int updateBpmProcessDefinition(BpmProcessDefinition bpmProcessDefinition)
    {

        bpmProcessDefinition.setUpdateTime(DateUtils.getNowDate());

        //如果processKey是新的就插入新数据
        if(bpmProcessDefinition.getProcessKey()!=null && !bpmProcessDefinition.getProcessKey().isEmpty()){
            BpmProcessDefinition bpmProcessDefinitionOld = bpmProcessDefinitionMapper.selectBpmProcessDefinitionByProcessKey(bpmProcessDefinition.getProcessKey());
            if(bpmProcessDefinitionOld==null){
                bpmProcessDefinition.setProcessKey(bpmProcessDefinition.getProcessKey());
                bpmProcessDefinition.setVersion(1L);

            return    bpmProcessDefinitionMapper.insertBpmProcessDefinition(bpmProcessDefinition);
            }else{
                bpmProcessDefinition.setId(bpmProcessDefinitionOld.getId());
                bpmProcessDefinition.setVersion(bpmProcessDefinitionOld.getVersion()+1);

                return bpmProcessDefinitionMapper.updateBpmProcessDefinition(bpmProcessDefinition);
            }
        }else{
            throw new RuntimeException("processKey不能为空");
        }


    }

    /**
     * 批量删除模版引擎管理
     * 
     * @param ids 需要删除的模版引擎管理主键
     * @return 结果
     */
    @Override
    public int deleteBpmProcessDefinitionByIds(Long[] ids)
    {
        return bpmProcessDefinitionMapper.deleteBpmProcessDefinitionByIds(ids);
    }

    /**
     * 删除模版引擎管理信息
     * 
     * @param id 模版引擎管理主键
     * @return 结果
     */
    @Override
    public int deleteBpmProcessDefinitionById(Long id)
    {
        return bpmProcessDefinitionMapper.deleteBpmProcessDefinitionById(id);
    }
}
